//
//  OrderProcessingActivityView.m
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/9.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "OrderProcessingActivityView.h"
#import <Masonry/Masonry.h>
#import <pop/pop.h>

@interface OrderProcessingActivityView()
@property(nonatomic, strong) UIImageView *greenCycleImageView;
@property(nonatomic, strong) UIImageView *redCycleImageView;
@property(nonatomic, strong) UIImageView *shadowCycleImageView;
@property(nonatomic, strong) UIImageView *successImageView;
@property(nonatomic, strong) UILabel *successLabel;
@property(nonatomic, strong) UILabel *countLabel;
@property(nonatomic, strong) UILabel *topLabel;
@property(nonatomic, strong) UILabel *bottomLabel;
@property(nonatomic, assign, getter=isFirst) BOOL first;
@end
@implementation OrderProcessingActivityView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)animationWithCount:(NSInteger)count {
    if (self.isFirst) {
        //首次
        _countLabel.text = [NSString stringWithFormat:@"%@",@(count)];
        self.first = NO;
        return;
    }
    //TODO 加入最小间隔时间
    [UIView transitionWithView:_countLabel duration:1.2 options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationCurveEaseInOut animations:^{
        _countLabel.text = [NSString stringWithFormat:@"%@",@(count)];
    } completion:^(BOOL finished) {
        if (count <= 0) {
            [self showGreenView];
            [UIView transitionWithView:_countLabel duration:1.2 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                _countLabel.text = nil;
            } completion:^(BOOL finished) {
                 [self showSuccessView];
                [self hiddenLabels];
            }];
        }
    }];
}

- (void)hiddenLabels {
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    animation.toValue = @(0);
    animation.duration = 0.5;
    [_topLabel pop_addAnimation:animation forKey:nil];
    [_bottomLabel pop_addAnimation:animation forKey:nil];
}

- (void)rotateRedView {
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerRotation];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.repeatForever = YES;
    animation.toValue = @(M_PI * 2);
    animation.duration = 0.5;
    [_redCycleImageView.layer pop_addAnimation:animation forKey:nil];
}

- (void)showGreenView {
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    animation.toValue = @(1.0);
    animation.duration = 1.5;
    [_greenCycleImageView pop_addAnimation:animation forKey:nil];
}

- (void)showSuccessView {
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    animation.toValue = @(1.0);
    animation.duration = 0.75;
    animation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
        [self resizeSuccessView];
    };
    [_successImageView pop_addAnimation:animation forKey:nil];
}

- (void)resizeSuccessView {
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    animation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.25, 1.25)];
    animation.duration = 0.75;

    POPBasicAnimation *reset = [POPBasicAnimation animationWithPropertyNamed:kPOPViewScaleXY];
    reset.toValue = [NSValue valueWithCGSize:CGSizeMake(1, 1)];
    reset.beginTime = CACurrentMediaTime() + 0.75;
    reset.duration = 0.75;
    reset.completionBlock = ^(POPAnimation *anim, BOOL finished) {
        [self hiddenRedView];
        [self shrinkSelf];
    };
    [_successImageView pop_addAnimation:animation forKey: nil];
    [_successImageView pop_addAnimation:reset forKey: nil];
}

- (void)hiddenRedView {
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    animation.toValue = @(0);
    animation.duration = 0.5;
    animation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
        if (self.animationsCompletion) {
            self.animationsCompletion();
        }
    };
    [_redCycleImageView pop_addAnimation:animation forKey:nil];

}

- (void)shrinkSelf {
    CGRect frame = self.layer.frame;
    self.layer.anchorPoint = CGPointMake(0.5, 0);
    self.layer.frame = frame;
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
    animation.toValue = [NSValue valueWithCGSize:CGSizeMake(0.5, 0.5)];
    animation.duration = 0.75;
    animation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
        [self hiddenRedView];
        [self showSuccessLabel];
    };
    [self.layer pop_addAnimation:animation forKey:nil];
}

- (void)showSuccessLabel {
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
    animation.toValue = @(1.0);
    animation.duration = 0.75;
    [_successLabel pop_addAnimation:animation forKey:nil];
}

- (void)setup {
    self.first = YES;
    self.shadowCycleImageView = [[UIImageView alloc] init];
    _shadowCycleImageView.image = [UIImage imageNamed:@"cycle_shadow"];
    [self addSubview:_shadowCycleImageView];

    self.greenCycleImageView = [[UIImageView alloc] init];
    _greenCycleImageView.alpha = 0;
    _greenCycleImageView.image = [UIImage imageNamed:@"cycle_green"];
    [self addSubview:_greenCycleImageView];

    self.redCycleImageView = [[UIImageView alloc] init];
    _redCycleImageView.image = [UIImage imageNamed:@"cycle_red"];
    [self addSubview:_redCycleImageView];

    self.successImageView = [[UIImageView alloc] init];
    _successImageView.alpha = 0;
    _successImageView.image = [UIImage imageNamed:@"cycle_success"];
    [self addSubview:_successImageView];

    self.countLabel = [[UILabel alloc] init];
    _countLabel.textColor = [UIColor blackColor];
    _countLabel.font = [UIFont fontWithName:@"AvenirNext-UltraLight" size:280];
    _countLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_countLabel];

    self.successLabel = [[UILabel alloc] init];
    _successLabel.textAlignment = NSTextAlignmentCenter;
    _successLabel.alpha = 0;
    _successLabel.text = @"订单处理完成";
    _successLabel.textColor = [UIColor colorWithHexString:@"#6bcd44"];
    _successLabel.font = [UIFont systemFontOfSize:30];
    [self addSubview:_successLabel];

    self.topLabel = [[UILabel alloc] init];
    _topLabel.textAlignment = NSTextAlignmentCenter;
    _topLabel.text = @"还有";
    _topLabel.textColor = RGBColorAlpha(25, 25, 25, 0.5);
    _topLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:_topLabel];

    self.bottomLabel = [[UILabel alloc] init];
    _bottomLabel.textAlignment = NSTextAlignmentCenter;
    _bottomLabel.text = @"个订单";
    _bottomLabel.textColor = RGBColorAlpha(25, 25, 25, 0.5);
    _bottomLabel.font = [UIFont systemFontOfSize:16];
    [self addSubview:_bottomLabel];

    [_shadowCycleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];

    [_greenCycleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];

    [_redCycleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];

    [_successImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];

    [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(self);
    }];

    [_successLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(_successImageView.mas_bottom).offset(10);
    }];

    [_topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(30);
    }];

    [_bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-30);
    }];

    [self rotateRedView];
}

@end

//"AvenirNext-Medium",
//"AvenirNext-DemiBoldItalic",
//"AvenirNext-DemiBold",
//"AvenirNext-HeavyItalic",
//"AvenirNext-Regular",
//"AvenirNext-Italic",
//"AvenirNext-MediumItalic",
//"AvenirNext-UltraLightItalic",
//"AvenirNext-BoldItalic",
//"AvenirNext-Heavy",
//"AvenirNext-Bold",
//"AvenirNext-UltraLight"

